package com.src.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.src.calendarItem.Event;
import com.src.calendarItem.Event.EventClass;

public class EventView extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextArea eventSum;
	
	private JTextField eventStartDate;
	private JButton pickStartDate;
	private JComboBox eventStartTime;

	private JTextField eventEndDate;
	private JButton pickEndDate;
	private JComboBox eventEndTime;
	
	private JTextArea eventLocation;
	private JTextArea eventDesc;
	
	private JComboBox eventCategory;
	private JComboBox eventClass;
	
	private String[] cate = {"Public","Personal","Meeting"};
	private String[] timeslot;
	
	private JPanel view;
	private Event event;

	/**
	 * This is the default constructor
	 */
	public EventView() {
		super();
		fillTimeSlot();
		initialize();
		buildEventView();
	}
	
	public EventView(Event ev)
	{
		super();
		fillTimeSlot();
		initialize();
		buildEventView();
		this.event = ev;
		setEventView(ev);
		
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new GridLayout(9,2,10,10));
		this.view = this;
	}
	
	private void buildEventView()
	{
		this.add(new JLabel("Event Category",JLabel.CENTER));
		eventCategory = new JComboBox(cate);
		this.add(eventCategory);
		
		this.add(new JLabel("Event Name",JLabel.CENTER));
		eventSum = new JTextArea();
		eventSum.setLineWrap(true);
		eventSum.setWrapStyleWord(true);
		
		JScrollPane eventsumPane = new JScrollPane(eventSum);
		
		this.add(eventsumPane);
		
		this.add(new JLabel("Event Start Date",JLabel.CENTER));
		eventStartDate = new JTextField();
		eventStartDate.setEditable(false);
		pickStartDate = new JButton(">>");
		pickStartDate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				PickDate pick = new PickDate(view,true);
				pick.setVisible(true);
			}
		});
		
		
		
		JPanel startDatePane = new JPanel(new BorderLayout());
		startDatePane.add(eventStartDate,BorderLayout.CENTER);
		startDatePane.add(pickStartDate, BorderLayout.EAST);
		
		this.add(startDatePane);
		
		// pick the event start time
		eventStartTime = new JComboBox(timeslot);
		this.add(new JLabel("Event Start Time",JLabel.CENTER));
		this.add(eventStartTime);
		
		
		this.add(new JLabel("Event End Date",JLabel.CENTER));
		eventEndDate = new JTextField();
		eventEndDate.setEditable(false);
		pickEndDate = new JButton(">>");
		pickEndDate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				PickDate pick = new PickDate(view,false);
				pick.setVisible(true);
			}
		});
		
		JPanel endDatePane = new JPanel(new BorderLayout());
		endDatePane.add(eventEndDate,BorderLayout.CENTER);
		endDatePane.add(pickEndDate, BorderLayout.EAST);
		
		this.add(endDatePane);
		
		eventEndTime = new JComboBox(timeslot);
		this.add(new JLabel("Event End Time",JLabel.CENTER));
		this.add(eventEndTime);
		
		this.add(new JLabel("Event Location",JLabel.CENTER));
		eventLocation = new JTextArea();
		
		eventLocation.setLineWrap(true);
		eventLocation.setWrapStyleWord(true);
		
		JScrollPane eventLocPane = new JScrollPane(eventLocation);
		
		this.add(eventLocPane);
		
		this.add(new JLabel("Event Description",JLabel.CENTER));
		eventDesc = new JTextArea();
		
		eventDesc.setLineWrap(true);
		eventDesc.setWrapStyleWord(true);
		
		JScrollPane eventDescPane = new JScrollPane(eventDesc);
		this.add(eventDescPane);
		
		
		this.add(new JLabel("Event Class",JLabel.CENTER));
		eventClass = new JComboBox(Event.EventClass.values());
		this.add(eventClass);
	}

	public String getEventSummary()
	{
		return eventSum.getText();
	}
	
	public String getEventStartDate()
	{		
		return eventStartDate.getText();
	}
	
	public String getEventStartTime()
	{
		String time = timeslot[eventStartTime.getSelectedIndex()];
		time = time.substring(0, 2)+time.substring(3)+"00";
		return time;
	}
	
	public String getEventEndDate()
	{
		return eventEndDate.getText();
	}
	
	public String getEventEndTime()
	{
		String time = timeslot[eventEndTime.getSelectedIndex()];
		time = time.substring(0, 2)+time.substring(3)+"00";
		
		return time;
	}
	
	public String getEventLocation()
	{
		return eventLocation.getText();
	}
	
	public String getEventDesc()
	{
		return eventDesc.getText();
	}
	
	public String getEventCategory()
	{
		return cate[eventCategory.getSelectedIndex()];
	}
	
	public EventClass getEventClass()
	{
		return (Event.EventClass)eventClass.getSelectedItem();
	}
	
	public void setEventStartDate(String date)
	{
		eventStartDate.setText(date);
	}
	
	public void setEventEndDate(String date)
	{
		eventEndDate.setText(date);
	}

	
	private void fillTimeSlot()
	{
		timeslot = new String[48];
		int count = 0;
		for(int i = 0; i<24; i++)
		{

				timeslot[count++]= ((i>=10)? String.valueOf(i):("0"+String.valueOf(i)))+":00";
				timeslot[count++] = ((i>=10)? String.valueOf(i):("0"+String.valueOf(i)))+":30";

		}
	}
	
	private void setEventView(Event ev)
	{
		this.eventSum.setText(ev.getSummary());
		this.eventStartDate.setText(ev.getStartDate());
		this.eventStartTime.setSelectedItem(ev.getStartTime().substring(0, 2)+":"+ ev.getStartTime().substring(2,4));
		this.eventEndDate.setText(ev.getEndDate());
		this.eventEndTime.setSelectedItem(ev.getEndTime().substring(0, 2)+":"+ev.getEndTime().substring(2,4));
		this.eventLocation.setText(ev.getLocation());
		this.eventCategory.setSelectedItem(ev.getCategory());
		this.eventDesc.setText(ev.getDescription());
		this.eventClass.setSelectedItem(ev.getEventClass());
	}
	
	public Event getEvent()
	{
		event.setSummary(this.getEventSummary());
		event.setCategory(this.getEventCategory());
		event.setStartDate(this.getEventStartDate());
		event.setStartTime(this.getEventStartTime());
		event.setEndDate(this.getEventEndDate());
		event.setEndTime(this.getEventEndTime());
		event.setLocation(this.getEventLocation());
		event.setDescription(this.getEventLocation());
		event.setEventClass(this.getEventClass());
		
		return event;
	}
}
